package com.scenic.rd_android.threerd.picasso;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.nova.scenic.projectlibs.util.StringUtil;
import com.nova.scenic.projectlibs.util.StringWrapper;
import com.nova.scenic.projectlibs.util.collection.Collections;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.nova.scenic.projectlibs.util.debug.ToastAlertUtil;
import com.nova.scenic.projectlibs.util.io.FileUtils;
import com.nova.scenic.projectlibs.util.io.StreamUtil;
import com.nova.scenic.projectlibs.widget.dialog.BaseDialog;
import com.scenic.rd_android.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class PicassoTest extends AppCompatActivity {

    MyLog myLog = new MyLog("PicassoTest");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picassor_test);
        ImageView imageView = (ImageView) findViewById(R.id.id_imageView_testPicasso);
        try {

            String[] imgPath = getAssets().list("rd_res_img");
            for (String str:imgPath) {
                InputStream input = getAssets().open("rd_res_img/" + str);
                StreamUtil.streamToFile(input,new File(getCacheDir(),str));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        Collections.showCollection(getCacheDir().list(), new Collections.OnShowCollectionHelper<String>() {
            @Override
            public void onItem(String s) {
                myLog.info("file " + s);
            }

            @Override
            public boolean onCloseInternalPrint() {
                return false;
            }
        });
        File [] files = getCacheDir().listFiles();

        Picasso.with(getApplicationContext()).load("/data/data/com.scenic.rd_android/cache/photo1.jpg").into(imageView);

        for (File file: files) {
//            Picasso.with(getApplicationContext()).load(Uri.parse("file:///" + file.getAbsolutePath() )).into(target);

            if(StringUtil.isImageSuffix(file.getAbsolutePath())){
                myLog.info("file path " + file.getAbsolutePath());
                Picasso.with(getApplicationContext()).load(file).into(
                        new InternalTarget(new StringWrapper(file.getAbsolutePath()).fileName().peek())
                );
                break;
            }
        }

    }

    class InternalTarget implements Target{
        String name;
        InternalTarget(String string){
            this.name = string;
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            myLog.info("onBitmapLoaded");
            new BaseDialog.ShowImageDialog(PicassoTest.this,name).showBitmap(bitmap).show();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    }

}
